//
//  ViewController.m
//  UITableView
//
//  Created by dllo on 16/6/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Contact.h"
#import "AddressBook.h"
#import "NewTableViewCell.h"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain)AddressBook *addressBook;
@property (nonatomic, retain)UITableView *tabView;

@end

@implementation ViewController
-(void)dealloc{
    [_tabView release];
    [_addressBook release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handDate];
    [self creatTabView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)config{
   
    self.addressBook = [[AddressBook alloc]init];
    self.navigationItem.title = @"通讯录";
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}
-(void)handDate{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Contact" ofType:@".plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    for (NSString *key in dictionary) {
        NSArray *arrayContact = [dictionary objectForKey:key];
        NSMutableArray *array = [[NSMutableArray alloc]init];
        for (NSDictionary *dic in arrayContact) {
            Contact *contact = [[Contact alloc]init];
            [contact setValuesForKeysWithDictionary:dic];
            [array addObject:contact];
        }
        [self.addressBook.dicOfContact setValue:array forKey:key];

    }
    
}

    

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.addressBook.dicOfContact.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *array = [self.addressBook getGroupNames];
    NSString *key = [array objectAtIndex:section];
    NSArray *arr = [self.addressBook.dicOfContact valueForKey:key];
    return arr.count;
}
-(NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return [self.addressBook getGroupNames];
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [[self.addressBook getGroupNames] objectAtIndex:section];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSArray *array = [self.addressBook getGroupNames];
    NSArray *arr = [self.addressBook.dicOfContact valueForKey:[array objectAtIndex:indexPath.section]];
    Contact *contact = [arr objectAtIndex:indexPath.row];
    cell.contact = contact;
    cell.imageView.layer.cornerRadius = self.tabView.rowHeight / 2;
    cell.imageView.layer.masksToBounds = YES;
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    return cell;
}

-(void)creatTabView{
    self.tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tabView];
    self.tabView.delegate = self;
    self.tabView.dataSource = self;
    self.tabView.rowHeight = 100;
    [self.tabView registerClass:[NewTableViewCell class] forCellReuseIdentifier:@"cell"];

}










@end
